package young.dao.impl;

import com.mysql.cj.x.protobuf.MysqlxCursor;
import young.Util.Sql;
import young.dao.UserDAO;
import young.pojo.UserDO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class UserDAOImpl  implements UserDAO {
    protected Connection con;
    protected PreparedStatement statement;
    protected ResultSet rs;
    Sql sql = new Sql();

    @Override
    public UserDO getByUsernameAndPwd(String username, String pwd) {
        UserDO userDO = null;
        //创建连接
        con = sql.getConnection();
        statement = null;
        rs = null;
        String sql = "select * from t_atm where atm_id=? and atm_pwd=?";
        //查询调取
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, username);
            statement.setObject(2, pwd);
            rs = statement.executeQuery();


            while (rs.next()) {
                userDO = new UserDO();
                userDO.setId(rs.getString(1));
                userDO.setPassword(rs.getString(2));
                userDO.setMoney(rs.getInt(3));


            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                if (statement != null) {
                    try {
                        statement.close();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                    if (rs != null) {
                        try {
                            rs.close();
                        } catch (SQLException throwables) {
                            throwables.printStackTrace();
                        }
                    }
                }
            }

        }
        return userDO;
    }

    @Override
    public void update(UserDO userDO1, UserDO userDO2) {
        // 1. 创建连接
        con = sql.getConnection();
        statement = null;
        String sql = "update t_atm set atm_pwd=?,atm_money=? where atm_id=?";
        try {
            // 2. 获取执行SQL语句对象
            con.setAutoCommit(false);
            statement = con.prepareStatement(sql);
            statement.setObject(1, userDO1.getPassword());
            statement.setObject(2, userDO1.getMoney());
            statement.setObject(3, userDO1.getId());
            statement.executeUpdate();

            statement.setObject(1, userDO2.getPassword());
            statement.setObject(2, userDO2.getMoney());
            statement.setObject(3, userDO2.getId());
            statement.executeUpdate();
            con.commit();

        } catch (SQLException throwables) {
            throwables.printStackTrace();

            try {
                con.rollback(); //异常回滚
            } catch (SQLException e) {
                e.printStackTrace();
            }


        } finally {
            // 4. 关闭连接
            guanbi(con, statement);

        }

    }

    @Override
    public UserDO getById(String id) {
        con = sql.getConnection();
        statement = null;
        rs = null;
        UserDO user=null;
        String sql = "select * from t_atm  where atm_id=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, id);
            rs = statement.executeQuery();

            if (rs.next()) {
                user = new UserDO();
                user.setId(rs.getString(1));
                user.setPassword(rs.getString(2));
                user.setMoney(rs.getInt(3));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            guanbi(con, statement, rs);
        }
        return user;
    }

    @Override
    public UserDO getPassword(String pwd) {
        UserDO userDO = null;
        //创建连接
        con = sql.getConnection();
        statement = null;
        rs = null;
        String sql = "select * from t_atm where  atm_pwd=?";
        //查询调取
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, pwd);
            rs = statement.executeQuery();


            while (rs.next()) {
                userDO = new UserDO();
                userDO.setId(rs.getString(1));
                userDO.setPassword(rs.getString(2));
                userDO.setMoney(rs.getInt(3));


            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                if (statement != null) {
                    try {
                        statement.close();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                    if (rs != null) {
                        try {
                            rs.close();
                        } catch (SQLException throwables) {
                            throwables.printStackTrace();
                        }
                    }
                }
            }

        }
        return userDO;

    }

    private void guanbi(Connection con, PreparedStatement statement, ResultSet rs) {
       if (con!=null){
           try {
               con.close();
           } catch (SQLException throwables) {
               throwables.printStackTrace();
           }
       }if (statement!=null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }if (rs!=null){
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    private void guanbi(Connection con, PreparedStatement statement) {
       if (con!=null){
           try {
               con.close();
           } catch (SQLException throwables) {
               throwables.printStackTrace();
           }
       }if (statement!=null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }


}
